var SwordToolBar = new Class({
    Implements : [ Events, Options ],
    name:"SwordToolBar",
    $family: {name: 'SwordToolBar'},
    options : {
        name : "",
        type : "",
        pNode : null,
        isExtend : "true",
        showType : "mini"  //显示类型：mini / normal,默认normal
        ,bindTo : null //将mini的toolbar嵌入到form或grid中
        ,widgetName:null
        ,checkName:null
        ,isFixed :'false'  //是否浮动
    },
    associate : null,
    containerBuffer : null,
    enabledStatus : null,
    onAfterInit : $empty,
    itemEvents:[],
    initParam : function(initPara) {
        this.htmlOptions(initPara);
        this.initToolBar();
        this.onAfterInit();
    },
    initToolBar : function() {
        var child = this.pNode().getChildren();
        var container = this.options.showType == 'normal'?this.pNode().getElements("div[class='tb_box'][name='container']"):this.pNode().getElements("div[class='tb_mini_box'][name='container']");
        
        if(this.options.isFixed=='true'&&!this.options.bindTo){
        	container.addClass('toolbar_fixed');
        }
        var associateDiv = this.pNode().getElement("div[name='associate']");
        pc.getPageInit().addEvent('onDataInit', function() {
            this.getAssociate(associateDiv);
        }.bind(this));
        if (child.length != 0) {
            child.each(function(item) {
            	if(item.get("class")=="tb_box")return;
                var name = item.get('name');
                var type = item.get('type');

                item.set("gridname",this.options.widgetName);
                
                var itemO = this.items[name];
                if (itemO != null) {
                    this.creatDefualtButton(
                            item, name, type,
                            itemO['pclass'],
                            itemO['caption'],
                            itemO['disable']);
                } else {
                    var checkPro = this.checkPro(item);
                    if (checkPro) {
                        this.create(item);
                    } 
                }
            }.bind(this));
        }
        var wn = this.options.bindTo;
    	var w = $w(wn);//toolbar必须定义在绑定组件的下面
    	if(wn && this.options.showType=='mini' && w){
    		if('SwordForm'==w.name)this.pNode().inject(w.wrapDiv.getElement('div.swordform-panel-box'),'after');
            else if('SwordGrid'==w.name){
            	this.pNode().inject(w.scrollDiv,'before');
            	//container.setStyles({'border':'none','border-bottom':'1px solid #7F9DB9'});
            }
    	}
    	if(this.options.isFixed=='true'&&!this.options.bindTo){
	    	this.pNode().adopt(new Element('div', {
	    		'styles': {
	    			'height':this.options.showType=='mini'?'40px':'69px'
	            	}
	        }));
    	}
        this.containerBuffer = container;
        this.setEnabledStatus(container);
    },
    setEnabledStatus : function(container) {
        this.enabledStatus = new Object();
        if (container.getElement("div[name='box']").getChildren().length != 0) {
            container.getElement("div[name='box']").getChildren().each(
                    function(item) {
                        this.enabledStatus[item.get('name')] = item
                                .get('enabled');
                    }.bind(this));
        }
    },
    initStatus : function(obj) {
    	try{
	    	obj.disable('open',true);
	    	obj.disable('edit',true);
	    	obj.disable('delete',true);
    	}catch(e){
	    	var toolbar = $w(obj.pNode.get("name"));
	    	toolbar.disable('open',true);
	    	toolbar.disable('edit',true);
	    	toolbar.disable('delete',true);
    	}
        /*if ($chk(obj.containerBuffer)) {//sword5 注释掉了 不知道干啥的 
            obj.containerBuffer.getElement("div[name='box']").getChildren().each(function(item) {
                obj.enabledStatus[item.get('name')] == 'true' ? obj.setEnabled(item.get('name')) : obj.setDisabled(item.get('name'));
            }.bind(obj));
        }*/
    },
    pNode : function() {
        return this.options.pNode;
    },
    checkPro : function(obj) {
        return $chk(obj.get('name')) && $chk(obj.get('caption'));
    },
    getAssociate : function(assDiv) {
    	if(this.options.widgetName){
    		this.associate = new Object();
    		var widgetName = this.options.widgetName;
            var widgetObj = this.loadWidget(widgetName);
            var widgetType = widgetObj.$family.name;
            var acccociateType = this.associate[widgetType];
            if (!$chk(acccociateType)) {
                acccociateType = (this.associate[widgetType] = {});
            }
            this.build( widgetType, widgetName);
            this.regAssociateEvent(widgetType, widgetName);
    	}
        /*if ($chk(assDiv)) {
            this.associate = new Object();
            var widgets = assDiv.getElements("div[name='associateWighet']").length != 0 ? assDiv.getElements("div[name='associateWighet']") : assDiv.getElements("div[name='associateWidget']");
            widgets.each(function(widget, index) {
                var widgetName = widget.get("widgetName");
                var widgetObj = this.loadWidget(widgetName);
                var widgetType = widgetObj.name;
                var acccociateType = this.associate[widgetType];
                if (!$chk(acccociateType)) {
                    acccociateType = (this.associate[widgetType] = {});
                }
                this.build(widget, widgetType, widgetName);
                this.regAssociateEvent(widgetType, widgetName);
            }.bind(this));
        } else {
            if (this.alarm) {
                alert('请设置ToolBar关联的组件！');
            }
        }*/
    },
    regAssociateEvent : function(widgetType, widgetName) {
        var widget = $w(widgetName);
        if (this.associate != null) {
            var acc = this.associate[widgetType][widgetName];
            if ($chk(acc)) {
                for (var key in acc.events) {
                    if (key) {
                        var funcArray = this.getFunc(acc.events[key]);
                        for (var i = 0; i < funcArray.length; i++) {
                            var func = funcArray[i];
                            widget.addEvent(key, func.bind(this, [widgetName]));
                        }
                    }
                }
                if (widgetType == "SwordGrid") {
                    if ($chk(acc.muiltCheckFunction)) {
                        widget.addEvent('onAfterCreateHeader', function() {
                            var allCheck = widget.header().getElement('div[_for=' + acc.muiltCheckName + ']').getElement('input');
                            if ($chk(allCheck)) {
                                allCheck.addEvent('click', acc.muiltCheckFunction.bind(this, [allCheck]));
                            }
                        }.bind(this));
                    }
                }
            }
        }
    },
//    getSwordGridAllCheckBox : function(widgetName, checkName) { sword5
//        return ($w(widgetName).header().getElement(
//                'div[_for=' + checkName + ']').getElement('input'));
//    },
    loadWidget : function(wighetName) {
        var widgetObject = pc.getWidget(wighetName);
        if ($chk(widgetObject)) {
            return widgetObject;
        } else {
            if (this.alarm) {
                alert('ToolBar组件无法获取关联组件' + wighetName);
            }
        }
    },
    buttonEvents : {
        "onClick" : {
            "SwordGrid" : {
                "firstPage" : function(wighetName) {
                    var w = $w(wighetName);
                    w.loadPage(1);
                    this.initStatus(this);
                },
                "endPage" : function(wighetName) {
                    var w = $w(wighetName);
                    w.loadPage(w.totalPage());
                    this.initStatus(this);
                },
                "nextPage" : function(wighetName) {
                    var w = $w(wighetName);
                    w.loadPage(w.pageNum() + 1);
                    this.initStatus(this);
                },
                "previousPage" : function(wighetName) {
                    var w = $w(wighetName);
                    w.loadPage(w.pageNum() - 1);
                    this.initStatus(this);
                }
            }
        }
    },
    wighetAssociates : {
        "SwordGrid" : {
            event : {
                "onRowClick" : function() {
                    this.rowClick(this);
                },
                "onAfterInitData" : function() {
                    this.initStatus(this);
                },
                onAllCheckClick:function(){
                	this.allCheckClick(this);
                }
            }
        },
        "SwordForm" : null,
        "SwordTree" : {
			event : {
				"onNodeClick" : function() {
					this.nodeClick(this);
				},
				"onFinish" : function() {
					this.initStatus(this);
				},
				"onAfterLoadData" : function(){
					this.afterLoadData(this);
				}
			}
		}
    }
});
SwordToolBar.implement({
    events : {
        "SwordGrid" : {
            "onRowClick" : "onRowClick",
            "onAfterInitData" : "onAfterInitData"
            ,onAllCheckClick:"onAllCheckClick"
        },
		"SwordTree" : {
			"onNodeClick" : "onNodeClick",
			"onFinish" : "onFinish",
			"onAfterLoadData" : "onAfterLoadData"
		}
    }
});
SwordToolBar.implement({
    items : {
        "edit" : {
            "caption" : i18n.toolEdit,
            "type" : "edit",
            "pclass" : "edit",
            "enabled" : "false"
        },
        "new" : {
            "caption" : i18n.toolNew,
            "type" : "new",
            "pclass" : "new",
            "enabled" : "true"
        },
        "delete" : {
            "caption" : i18n.toolDel,
            "type" : "delete",
            "pclass" : "delete",
            "enabled" : "false"
        },
        "refresh" : {
            "caption" : i18n.toolFresh,
            "type" : "refresh",
            "pclass" : "refresh",
            "enabled" : "true"
        },
        "open" : {
            "caption" : i18n.toolOpen,
            "type" : "open",
            "pclass" : "open",
            "enabled" : "false"
        },
        "find" : {
            "caption" : i18n.toolFind,
            "type" : "find",
            "pclass" : "find",
            "enabled" : "true"
        },
        "firstPage" : {
            "caption" : i18n.firstPage,
            "type" : "firstPage",
            "pclass" : "firstPage",
            "enable" : "true"
        },
        "endPage" : {
            "caption" : i18n.endPage,
            "type" : "endPage",
            "pclass" : "endPage",
            "enable" : "true"
        },
        "nextPage" : {
            "caption" : i18n.nextPage,
            "type" : "nextPage",
            "pclass" : "nextPage",
            "enable" : "true"
        },
        "previousPage" : {
            "caption" : i18n.previousPage,
            "type" : "previousPage",
            "pclass" : "previousPage",
            "enable" : "true"
        }
        ,
        "save" : {
            "caption" : i18n.toolSave,
            "type" : "save",
            "pclass" : "save",
            "enabled" : "true"
        },
        "back" : {
            "caption" : i18n.toolBack,
            "type" : "back",
            "pclass" : "back",
            "enable" : "true"
        },
        "export" : {
            "caption" : i18n.toolExport,
            "type" : "export",
            "pclass" : "export",
            "enable" : "true"
        },
        "addtime" : {
            "caption" : i18n.toolAddtime,
            "type" : "addtime",
            "pclass" : "addtime",
            "enable" : "true"
        },
        "reducetime" : {
            "caption" : i18n.toolReducetime,
            "type" : "reducetime",
            "pclass" : "reducetime",
            "enable" : "true"
        }
        ,
        "addright" : {
            "caption" : i18n.toolAddright,
            "type" : "addright",
            "pclass" : "addright",
            "enable" : "true"
        },
        "reduceright" : {
            "caption" : i18n.toolReduceright,
            "type" : "reduceright",
            "pclass" : "reduceright",
            "enable" : "true"
        },
        "close" : {
            "caption" : '关闭',
            "type" : 'close',
            "pclass" : "close",
            "enable" : "true"
        },
        "print" : {
            "caption" : '打印',
            "type" : "print",
            "pclass" : "print",
            "enable" : "true"
        },
        "revoke" : {
            "caption" : '废弃',
            "type" : "revoke",
            "pclass" : "revoke",
            "enable" : "true"
        }
    }
});
SwordToolBar.implement({
    create : function(obj) {
        var name=obj.get('name');
		if (!$chk(name))return;
        var itemEvent = new ItemEvent();
        itemEvent.initParam(obj);
        itemEvent.pNode = this.options.pNode;
        this.itemEvents.include(itemEvent);
        var newDoc;
        if(this.options.showType!='mini'){
        	newDoc = this.pNode().getElements("div.tb[name='"+name+"']");
        }else{
        	newDoc = this.pNode().getElements("div.mini_tb[name='"+name+"']");
        }
        newDoc.addEvent('click', function() { 
        	if (newDoc.get('disable')!="true") {
                itemEvent.regOnClick();
            }
        });
        newDoc.addEvent('mouseover', function() {
            if (newDoc.get('disable')!="true") {
                itemEvent.regOnMouseOver();
            }
        });

        newDoc.addEvent('mouseout', function() {
            if (newDoc.get('disable')!="true") {
                itemEvent.regOnMouseout();
            }
        });
        
        
        return newDoc;
    },
    creatDefualtButton : function(obj, name, type, pclass, caption, enabled) {
        var itemEvent = new ItemEvent();
        itemEvent.initParam(obj);
        itemEvent.buttonEvents = this.buttonEvents;
        itemEvent.pNode = this.options.pNode;
        itemEvent.initStatus = this.initStatus.bind(this);
        this.itemEvents.include(itemEvent);
        var newDiv;
        if(this.options.showType!='mini'){
        	newDiv = this.pNode().getElements("div.tb[name='"+name+"']");
        }else{
        	newDiv = this.pNode().getElements("div.mini_tb[name='"+name+"']");
        }
        newDiv.addEvent('click', function() {
        	if (newDiv.get('disable')!="true") {
                itemEvent.regOnClick();
            }
        });
        newDiv.addEvent('mouseover', function() {
            if (newDiv.get('disable')!="true") {
                itemEvent.regOnMouseOver();
            }
        });
        newDiv.addEvent('mouseout', function() {
            if (newDiv.get('disable')!="true") {
                itemEvent.regOnMouseout();
            }
        });
        return newDiv;
    },
    disable : function(strName,useType) {
        var container = this.options.pNode.getElements("div[name='container']");
        var serach='name';
        if(useType==true){
        	serach='type';
        }
        var parentDivs = container.getElements('div['+serach+'='+strName+']')[0];
        if (parentDivs.length == 0)return null;
        var i=0;
        for(i=0;i<parentDivs.length;i++){
           var  parentDiv=parentDivs[i];
            parentDiv.set('disable', 'true');
            parentDiv.removeClass('tb_mini_a_enabled');
            var imageDiv = parentDiv.getElements("div[name='image']")[0];
            var imageClass = (imageDiv.get('class')).replace('_enabled', '_disabled');
            var imagePath = null;
            if ($chk(imagePath)) {
                var pathSuffix = imagePath.substring(imagePath.lastIndexOf('.') + 1);
                var pathStr = imagePath.substring(0, imagePath.lastIndexOf('.'));
                var newPath = pathStr.replace('_disabled', '') + '_disabled.' + pathSuffix;
                imageDiv.setStyles({'background-image' : 'url("' + newPath + '")'});
            } else {
                imageDiv.set('class', imageClass);
            }
            var captionSpan = parentDiv.getElements("div[name='caption']")[0];
            captionSpan.set('class', this.options.showType != 'mini' ? this.globe.text.disabled : this.mini_globe.text.disabled);
        }

    },
    enable : function(strName,useType) { 
        var container = this.options.pNode.getElements("div[name='container']");
        var serach='name';
        if(useType==true){
        	serach='type';
        }
        var parentDivs = container.getElements('div['+serach+'='+strName+']')[0];
        if (parentDivs.length == 0)return null;

        var i=0;
        for(i=0;i<parentDivs.length;i++){
            var parentDiv=parentDivs[i];
            parentDiv.set('disable', 'false');
            var imageDiv = parentDiv.getElements("div[name='image']")[0];
            var imageClass = (imageDiv.get('class')).replace('_disabled', '_enabled');
            var imagePath = null;
            if ($chk(imagePath)) {
                var pathSuffix = imagePath.substring(imagePath.lastIndexOf('.') + 1);
                var pathStr = imagePath.substring(0, imagePath.lastIndexOf('.'));
                var newPath = pathStr.replace('_disabled', '') + '.' + pathSuffix;
                imageDiv.setStyles({'background-image' : 'url("' + newPath + '")'});
            } else {
                imageDiv.set('class', imageClass);
            }
            var captionSpan = parentDiv.getElements("div[name='caption']")[0];
            captionSpan.set('class', this.options.showType != 'mini' ? this.globe.text.enabled : this.mini_globe.text.enabled);
        }
    },
    disableType : function(strType) {
        return this.disable(strType,true);

    }, 
    enableType : function(strType) {
    	return this.enable(strType,true);
    }
    ,show : function(strName) {
        var container = this.options.pNode.getElements("div[name='container']");
        var parentDiv = container.getElements("div[name='" + strName + "']");
        if (parentDiv.length == 0) {
            return null;
        }
        var buttonDiv = parentDiv[0];
        buttonDiv.setStyle('display', '');
    },
    hide : function(strName) {
        var container = this.options.pNode.getElements("div[name='container']");
        var parentDiv = container.getElements("div[name='" + strName + "']");
        if (parentDiv.length == 0)return null;
        var buttonDiv = parentDiv[0];
        buttonDiv.setStyle('display', 'none');
    }
});
SwordToolBar.implement( {
	globe : {
		box : {
			backGround : "tb_box",
			top : "",
			bottom : "",
			left : "l",
			right : "r",
			back : "c"
		},
		button : {
			backGround : "tb",
			top : "",
			bottom : "",
			left : "",
			right : ""
		},
		text : {
			enabled : "text_enabled",
			disabled : "text_disabled"
		},
		blankButtonDiv : "tb_blank"
	},
	mini_globe :{
		box : {
			backGround : "tb_mini_box",
			top : "",
			bottom : "",
			left : "l",
			right : "r",
			back : "c"
		},
		button : {
			backGround : "mini_tb",
			top : "",
			bottom : "",
			left : "",
			right : ""
		},
		text : {
			enabled : "text_enabled",
			disabled : "text_disabled"
		}
	}
	,
	defaultCss : {
		"new" : "new",
		"open" : "open",
		"find" : "find",
		"delete" : "delete",
		"refresh" : "refresh"
	}
});
SwordToolBar.implement({
    build : function( wighetType, wighetName) {
        if (wighetType == 'SwordGrid') {
            //var swordGridEvents = this.events['SwordGrid'];
            var accWighet = this.associate[wighetType][wighetName];
            if (!$chk(accWighet)) {
                accWighet = (this.associate[wighetType][wighetName] = {});
            }
            this.associate[wighetType][wighetName].muiltCheckName = 
            	this.options.checkName;

            var accEvents = accWighet.events;
            if (!$chk(accEvents)) {
                accEvents = (accWighet.events = {});
            }
            if (this.options.isExtend == 'true') {
                this.pluginBuild(wighetType, wighetName);
            }
            /*for (var key in swordGridEvents) {
                var tempEvent = wighetDiv.get(key);
                if ($chk(tempEvent)) {
                    if ($chk(this.events[wighetType][key])) {
                        this.associate[wighetType][wighetName].events[this.events[wighetType][key]] = tempEvent;
                    }
                }
            }*/
        } else if (wighetType == 'SwordTree') {
			var accWighet = this.associate[wighetType][wighetName];
			if (!$chk(accWighet)) {
				accWighet = (this.associate[wighetType][wighetName] = {});
			}
			var accEvents = accWighet.events;
			if (!$chk(accEvents)) {
				accEvents = (accWighet.events = {});
			}
			if (this.options.isExtend == 'true') {
				this.pluginBuild(wighetType, wighetName);
			}
		}
    },
    pluginBuild : function(wighetType, wighetName) {
        var wighetDefaultEvent = this.wighetAssociates[wighetType].event;
        for (var key in wighetDefaultEvent) {
            this.associate[wighetType][wighetName].events[this.events[wighetType][key]] = wighetDefaultEvent[key];
        }
       if (wighetType == "SwordGrid") {
			var wighetMuiltCheck = this.gridMuiltCheck;
			if ($chk(wighetMuiltCheck)) {
				this.associate[wighetType][wighetName].muiltCheckFunction = wighetMuiltCheck;
			}
		}
    }
});
SwordToolBar.implement( {
	associate : {
		"SwordGrid" : {},
		"SwordForm" : {},
		"SwordSubmit" : {},
		"SwordTree" : {}
	}
});
var ItemEvent = new Class({
    Implements : [ Events, Options ],
    options : {
        '_onClick' : null,
        'onClick' : null,
        'onBlur' : null,
        'onMouseover' : null,
        'onMouseout' : null,
        'name' : "",
        'type' : "",
        'gridName' : ""
    },
    buttonEvents : null,
    pNode : null,
    initStatus : $empty,
    earseEvent:function(el, events) {
        events.each(function(e) {
            var f = el.get(e);
            if(f) {
                if(Browser.Engine.trident4 || Browser.Engine.trident5) {
                    f += '';
                    f = f.substring(f.indexOf('{') + 1, f.lastIndexOf('}'));
                }
                el.set('_' + e, f);
                el.erase(e);
            }
        });
    },
    initParam : function(initPara) {
    	this.earseEvent(initPara,['onclick']);
        this.htmlOptions(initPara);
        if (!$chk(this.options.type)) {
            this.options.type = this.options.name;
        }
        pc.getPageInit().addEvent('onDataInit', function() {
            if ($chk(this.options.gridName)) {
                if (!$chk(this.options.onClick)) {
                    if ($chk(this.buttonEvents)) {
                        for (var key in this.buttonEvents) {
                            var wName = this.buttonEvents[key];
                            var events = wName[$w(this.options.gridName).name];
                            var event = events[this.options.type];
                            if ($chk(event)) {
                            	this.addEvent(key, event.bind(this, [ this.options.gridName ]));
                            }
                        }
                    }
                }
            }
            if (!$chk(this.options.onMouseover)) { 
                this.addEvent('onMouseover', function() {
                    var div = this.pNode.getElements("div[name='container']").getElements("div[name='"
                            + this.options.name + "']")[0];
                    var docClass = div.get('class')[0];
                    var newDocClass;
                    if(this.pNode.get('showType')!='mini'){
                        newDocClass = docClass.replace(" tb_a_enabled", "") + ' tb_a_enabled';
                    }else{
                    	newDocClass = docClass.replace(" tb_mini_a_enabled", "") + ' tb_mini_a_enabled';
                    }
                    div.set({'class' : newDocClass});
                }.bind(this));
            }
            if (!$chk(this.options.onMouseout)) {
                this.addEvent('onMouseout', function() {
                    var div = this.pNode.getElements("div[name='container']").getElements("div[name='"
                            + this.options.name + "']")[0];
                    var docClass = div.get('class')[0];
                    var newDocClass;
                    if(this.pNode.get('showType')!='mini'){
                        newDocClass = docClass.replace(" tb_a_enabled", "");
                    }else{
                        newDocClass = docClass.replace(" tb_mini_a_enabled", "");
                    }
                    div.set({'class' : newDocClass});
                }.bind(this));
            }
        }.bind(this));
    },
    regOnClick : function() {
    	if($defined(this.options._onClick))
            this.getFunc(this.options._onClick)[0]();
    	else if($defined(this.options.onClick)){
    		this.getFunc(this.options.onClick)[0]();
    	}else if($defined(this.buttonEvents) && this.options.gridName){
    		var wName = this.buttonEvents["onClick"];
            var events = wName[$w(this.options.gridName).name];
            var event = events[this.options.type];
            if ($chk(event)) {
            	event.call(this, [ this.options.gridName ]);
            }
        }
//    	this.fireEvent('onClick');
    },
    regOnMouseOver : function() {
        this.fireEvent('onMouseover');
    },
    regOnMouseout : function() {
        this.fireEvent('onMouseout');
    }
});


/******************************
 * SwordToolBar用户配置
 * 此类用于用户自定义配置默认按钮，按纽事件
 * 组件关联，组件关联响应事件。
 * 
 * @author wjl
 * 
 *****************************/

SwordToolBar.implement( {
	
	/**
	 * 用户开发提示开关
	 */
	alarm : false,
	
	/**
	 * 用户创建默认按钮对象
	 * 按照如下方式进行填写。
	 * 
	 * buttonName : {
	 * 		"captioin" : "按钮显示文字",
	 * 		"pclass" : "按钮class样式，命名解析规则为：按钮名称+状态，比如
	 * 					名为new的按钮，那么它的实际样式为new_enabled或者new_disabled
	 * 					此样式定义在default.css里面。",
	 * 		"enable" : "默认状态，当为true的时候，状态为可用，false的时候为置灰，将不
	 * 					注册按钮事件。"
	 * }
	 * 
	 */
	items : {
	// "firstPage" : {
	// "caption" : "首页",
	// "pclass" : "firstPage",
	// "enable" : "true"
	// }
	},

	/**
	 * 用户创建按钮与组件关联事件
	 * 填写方式：
	 * 
	 * "事件名称" : {
	 * 		"组件类型" : {
	 * 				"按钮名称" : 事件function
	 * 		}
	 * }
	 */
	buttonEvents : {
	// "onClick" : {
	// "SwordGrid" : {
	// "firstPage" : function(wighetName) {
	// var w = $w(wighetName);
	// w.loadPage(1);
	// this.initStatus(this);
	// }
	},

	/**
	 * 此接口用于当SwordToolBar初始化完成以后执行的事件。
	 * 根据需求进行配置，可以在用例页面直接重写此方法。
	 * $w('test').onAfterInit = function(){
	 * 		alert();
	 * }
	 * 此接口可以使用初始化组件代替。
	 */
//	 onAfterInit : function() {
//		this.setHide('new');
//	},

	/**
	 * SwordGrid组件多选框点击事件配置
	 * SwordToolbar会对所有组件注册checkBox的点击事件。
	 * 以下对象仅仅为事例，具体实现根据业务需求操作。
	 * 高级操作可以在业务页面直接重写此对象。
	 */
	gridMuiltCheck : function(allCheckBox) {
		this.muiltCheckClick(this,allCheckBox);
	},

	/**
	 * 表格行点击响应事件
	 * 参数说明：
	 * obj：SwordToolBar对象
	 * wn: SwordToolBar关联表格名称
	 * data: 单击表格行数据
	 * row: 点击表格行
	 * e: 点击表格行事件
	 */
	rowClick : function(obj, wn, data, row, e) {
		obj.defaultGridCheckAction(wn,obj);
	},

	nodeClick : function(obj, wn, node) {
		obj.defaultTreeSelectAction(obj, wn);
	},
	
	afterLoadData : function(obj, wn){
		obj.defaultTreeSelectAction(obj, wn);
	},
	
	allCheckClick:function(obj,wn,check,el){
		if(!$chk(el))el = $w(wn).getHeaderCheckboxByName(obj.associate['SwordGrid'][wn].muiltCheckName);
		if(el.checked){
			obj.enableType('delete');
		}else{
			obj.disableType('delete');
		}
		obj.disableType('edit');
		obj.disableType('open');
	},
	
	muiltCheckClick : function(obj,allCheckBox) {
		if (allCheckBox.checked) {
			this.defaultGridMuiltCheckAction(obj);
		} else if(!allCheckBox.checked){
			obj.disableType('delete');
		}
	},
	
	defaultGridMuiltCheckAction:function(obj){
		obj.disableType('open');
		obj.disableType('edit');
		obj.enableType('delete');
	},
	
	defaultGridCheckAction : function(wn,obj) {
		var checkName = obj.associate['SwordGrid'][wn].muiltCheckName;
		var w = $w(wn);
		if ($chk(w)) {
			var checkRow = w.getCheckedRowData(checkName);
			if (checkRow.length < 1) {
				obj.disableType('open');
				obj.disableType('edit');
				obj.disableType('delete');
			} else if (checkRow.length == 1) {
				obj.enableType('open');
				obj.enableType('delete');
				obj.enableType('edit');
			} else if (checkRow.length > 1) {
				obj.disableType('open');
				obj.disableType('edit');
				obj.enableType('delete');
			}
		}
	},
	defaultTreeSelectAction : function(obj, wn) {
		var selectNode = $w(wn).getSelectedNode();
		if (!selectNode) {
			obj.disableType('open');
			obj.disableType('edit');
			obj.disableType('delete');
		} else if (selectNode) {
			obj.enableType('open');
			obj.enableType('delete');
			obj.enableType('edit');
		}
	}
});
